let db =require('../db');

//登录界面token 存入数据库

exports.insertToken  = async function (token,tokenDate) {
let sql=`
INSERT  INTO login_token(token,createdAt)
VALUES(:token,:tokenDate)
`
let data  =await db.query(sql,{token,tokenDate});
return data[0];    
}

//验证login token  

exports.validationToken  =  async function(token){
    let sql=`
    SELECT * FROM  login_token  where token =:token
    `
    let data = await db.query(sql,{token});
    return data[0];
}

//验证用户是否存在
exports.validationUser = async function(userName){
 let sql =`
 SELECT * FROM  admin WHERE userName=:userName
 `
 let data  = await db.query(sql,{userName});
 return data[0];
}

//添加token

exports.updateToken = async function(id,token){
 let sql=`
 UPDATE  admin SET 
 token =:token
 where id =:id
 `
 let data = await db.query(sql,{id,token});
 return data[0];
}


//验证token  是否存在

exports.getUserByApiToken  =async function(userName,token){
let sql=`
SELECT *FROM  admin  where  userName=:userName AND  token = :token
`
let data = await db.query(sql,{userName,token});
return data[0];
}

//根据手机号查询用户是否存在

exports.getUserByPhone  = async function(phone){
   let  sql=`
   SELECT  id,userName,phone,status FROM  admin  where phone=:phone AND  isdelete ='n'
   ` 
   let  data  = await db.query(sql,{phone});
   return data[0];
}

//重置用户密码

exports.updatePwd = async  function(userId,newPwd){
let  sql=`
  UPDATE  admin set password=:newPwd
  where id =:userId
`
let  data  = await db.query(sql,{userId,newPwd});
return data[0];
}